<?php

namespace App\Http\Controllers\Back;

use App\Http\Service\ServiceRegion;
use App\Model\Region;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;

class RegionController extends Controller
{

    private function leftBar() {
        return [
            'title' => '地区管理',
            'list'  => [
                [
                    'title' => '地区列表',
                    'type' => 'scenery_region_list',
                    'url' => url('back/region'),
                ],
            ]
        ];
    }

    /**
     * @param Request $request
     * czs
     */
    public function __construct(Request $request){
        $this->request = $request;
        $this->limit = config('config.pageLimit');
        $leftBarArr = $this->leftBar();
        view()->share(['title' => $leftBarArr['title'],'list'=>$leftBarArr['list'],'limit'=>$this->limit]);
    }

    /**
     * Display a listing of the resource.
     * 地区列表
     * @return \Illuminate\Http\Response
     * czs
     */
    public function index()
    {
        $keyWord = $this->request->input('keyword');
        $res = Region::getList($keyWord,$this->limit)->getData();
        $this->request->flash();
        return view('back/region/index')
            ->with('res',$res)
            ->with('titleType','scenery_region_list');
    }

    /**
     * Show the form for creating a new resource.
     * 添加景区地区分类
     * czs
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $res = Region::getByPidName($input["pid"], $input["name"])->getData();
            if (!$res->isEmpty()) {
            	$return = array(
            			'success' => false,
            			'msg' => "添加失败，地区名称已经存在，请重试！"
            	);
            } else {
            	$res = Region::create($input);
            	if ($res) {
            		$return = array(
            				'success' => true,
            				'msg' => "添加成功！"
            		);
            	} else {
            		$return = array(
            				'success' => false,
            				'msg' => "添加失败，请重试！"
            		);
            	}
            }
            return $return;
        } else {
            $res = Region::parentList()->getData();
            return view('back/region/create')
                ->with('res',$res)
                ->with('titleType','add_scenery_region');
        }
    }

    /**
     * 景区类型编辑保存
     * czs
     */
    public function edit($id)
    {
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            $res = Region::updateData($input, $id);
            return $res->getResult();
        } else {
            $res = Region::parentList()->getData();
            $one = Region::getOne($id)->getData();
            return view('back.region.edit')
                ->with('res',$res)
                ->with('one', $one);
        }
    }

    /**
     * Remove the specified resource from storage.
     * 删除分类
     * czs
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $del = SceneryRegion::deleteLine($id);
        return $del->getResult();
    }

    /**
     * 禁用、启用
     * czs
     */
    public function able($id,$able)
    {
        $res = SceneryRegion::able($id,$able);
        return $res->getResult();
    }
}
